<?php
    $model = new Application_Model_Common();
    $category_records = $model->getAllRecordDetail(TABLE_CATEGORY, "1", "category_name");
//    $subcategory_records = $model->getAllRecordDetail(TABLE_SUB_CATEGORY, "1", "subcategory_name");
    $country_records = $model->getAllRecordDetail(TABLE_COUNTRY, "1", "country_name");

    $event_type_value = "";
    $event_type_title = "Event";
    $category_title = "Category";
    $subcategory_title = "Subcategory";
    $category_value = "";
    $subcategory_value = "";
    $location = "";
//    $country_name = "";
//    $city_name = "";
    $query_value = "Search";
    if (isset($_SESSION["search"]))
    {
//        prd($_SESSION["search"]);
        if (!empty($_SESSION["search"]["event_type"]))
        {
            $event_type_value = $_SESSION["search"]["event_type"];
            $event_type_title = ucwords($_SESSION["search"]["event_type"]);
        }

        $category_title = "";
        if (!empty($_SESSION["search"]["category_name"]))
            $category_title = $_SESSION["search"]["category_name"];
        if (!empty($_SESSION["search"]["subcategory_name"]))
            $subcategory_title = $_SESSION["search"]["subcategory_name"];
        $category_value = $_SESSION["search"]["category_id"];
        $subcategory_value = $_SESSION["search"]["subcategory_id"];
        $location = $_SESSION["search"]["location"];
//        $country_name = $_SESSION["search"]["country_id"];
//        $city_name = $_SESSION["search"]["city_name"];
        if (!empty($_SESSION["search"]["query"]))
            $query_value = $_SESSION["search"]["query"];
        
        if(empty($category_title))
            $category_title="Category";

        $subcategory_records = array();
        if (!empty($category_title))
        {
            $subcategory_records = $model->getAllRecordFromLeftJoin(array("sc" => TABLE_SUB_CATEGORY), array("c" => TABLE_CATEGORY), "c.category_id = sc.category_id", "category_name = '$category_title'", "subcategory_name");
        }

//        unset($_SESSION["search"]);
    }
?>
<div class="h-nav">
    <form id="search-form" method="get" action="<?php echo $this->baseUrl("event/search"); ?>">
        <input type="hidden" name="getsearch" value="1"/>
        <div class="categories">
            <ul class="drop-list">
                <li class="select">
                    <a href="#" class="title"><?php echo $event_type_title; ?></a>
                    <div class="dropdown menu-list rbox">
                        <div class="item" rel="public">
                            <img class="fl thumb" src="<?php echo IMAGES_PATH; ?>/thumb1.3.jpg" alt="thumb" />
                            <div class="content">
                                <p><a href="public" rel="public" class="name">Public</a></p>
                                <p class="desc">Event</p>
                            </div>
                            <div class="cf"></div>
                        </div>
                        <div class="item" rel="private">
                            <img class="fl thumb" src="<?php echo IMAGES_PATH; ?>/thumb1.4.jpg" alt="thumb" />
                            <div class="content">
                                <p><a href="private" rel="private" class="name">Private</a></p>
                                <p class="desc">Event</p>
                            </div>
                            <div class="cf"></div>
                        </div>
                        <input type="hidden" name="event_type" value="<?php echo $event_type_value; ?>"/>
                    </div>
                </li>
                <li class="select">
                    <a href="#" class="title"><?php echo $category_title; ?></a>
                    <div class="dropdown menu-list rbox">
                        <?php
                            foreach ($category_records as $c_key => $c_value)
                            {
                                ?>
                                <div class="item search-box-categories" rel="<?php echo $c_value["category_id"]; ?>">
                                    <img class="fl thumb" src="<?php echo IMAGES_PATH; ?>/thumb1.1.jpg" alt="thumb" />
                                    <div class="content">
                                        <p><a href="<?php echo $c_value["category_id"]; ?>" rel="<?php echo $c_value["category_id"]; ?>" class="name"><?php echo $c_value["category_name"]; ?></a></p>
                                        <!--<p><a href="<?php echo $c_value["category_id"]; ?>" rel="<?php echo $c_value["category_id"]; ?>" class="name search-box-categories"><?php echo $c_value["category_name"]; ?></a></p>-->
                                        <!--<p class="desc"><?php echo $c_value["category_name"]; ?></p>-->
                                    </div>
                                    <div class="cf"></div>
                                </div>
                                <?php
                            }
                        ?>
                        <input type="hidden" name="category_id" value="<?php echo $category_value; ?>"/>
                    </div>
                </li>
                <li class="select">
                    <a href="#" class="title subcat-title"><?php echo $subcategory_title; ?></a>
                    <div class="dropdown menu-list rbox search-subcategories-here">
                        <?php
                            if (empty($subcategory_records))
                            {
                                ?>
                                <div class="item" rel="">
                                    <!--<img class="fl thumb" src="<?php echo IMAGES_PATH; ?>/thumb1.1.jpg" alt="thumb" />-->
                                    <div class="content">
                                        <p><a href="" rel="" class="name">select category</a></p>
                                        <!--<p class="desc"><?php echo $c_value["subcategory_name"]; ?></p>-->
                                    </div>
                                    <div class="cf"></div>
                                </div>
                                <?php
                            }
                            else
                            {
                                foreach ($subcategory_records as $key => $value)
                                {
                                    echo '<div class="item" rel="' . $value["subcategory_id"] . '">
                                    <img class="fl thumb" src="' . IMAGES_PATH . '/thumb1.1.jpg" alt="thumb" />
                                    <div class="content">
                                        <p><a href="' . $value["subcategory_id"] . '" rel="' . $value["subcategory_id"] . '" class="name search-box-categories subcats">' . $value["subcategory_name"] . '</a></p>
                                        <!--<p class="desc">' . $value["subcategory_name"] . '</p>-->
                                    </div>
                                    <div class="cf"></div>
                                </div>';
                                }
                            }
                        ?>
                        <input type="hidden" name="subcategory_id" id="subcat_id" value="<?php echo $subcategory_value; ?>"/>
                    </div>
                </li>
                <!--            <li class="select">
                                <a href="#" class="title">Subcategory</a>
                                <div class="dropdown menu-list rbox">
                                    <div class="item">
                                        <img class="fl thumb" src="<?php echo IMAGES_PATH; ?>/thumb1.1.jpg" alt="thumb" />
                                        <div class="content">
                                            <p><a href="#" class="name">Lorem Ipsum</a></p>
                                            <p class="desc">Lorem Ipsum dolor sit ...</p>
                                        </div>
                                        <div class="cf"></div>
                                    </div>
                                    <div class="item">
                                        <img class="fl thumb" src="<?php echo IMAGES_PATH; ?>/thumb1.1.jpg" alt="thumb" />
                                        <div class="content">
                                            <p><a href="#" class="name">Sit Amet Lorem Ips ...</a></p>
                                            <p class="desc">Lorem Ipsum dolor sit ...</p>
                                        </div>
                                        <div class="cf"></div>
                                    </div>
                                    <div class="item">
                                        <img class="fl thumb" src="<?php echo IMAGES_PATH; ?>/thumb1.2.jpg" alt="thumb" />
                                        <div class="content">
                                            <p><a href="#" class="name">Lorem Ipsum</a></p>
                                            <p class="desc">Lorem Ipsum dolor sit ...</p>
                                        </div>
                                        <div class="cf"></div>
                                    </div>
                                    <div class="item">
                                        <img class="fl thumb" src="<?php echo IMAGES_PATH; ?>/thumb1.3.jpg" alt="thumb" />
                                        <div class="content">
                                            <p><a href="#" class="name">Lorem Ipsum</a></p>
                                            <p class="desc">Lorem Ipsum dolor sit ...</p>
                                        </div>
                                        <div class="cf"></div>
                                    </div>
                                    <div class="item">
                                        <img class="fl thumb" src="<?php echo IMAGES_PATH; ?>/thumb1.4.jpg" alt="thumb" />
                                        <div class="content">
                                            <p><a href="#" class="name">Lorem Ipsum</a></p>
                                            <p class="desc">Lorem Ipsum dolor sit ...</p>
                                        </div>
                                        <div class="cf"></div>
                                    </div>
                                </div>
                            </li>-->
                <!--            <li class="select">
                                <a href="#" class="title">Country</a>
                                <div class="dropdown menu-list rbox">
                <?php
                    foreach ($country_records as $cy_key => $cy_value)
                    {
                        ?>
                                                                                                                                                                                                                            <div class="item" rel="<?php echo $cy_value["country_id"]; ?>">
                                                                                                                                                                                                                                <img class="fl thumb" src="<?php echo IMAGES_PATH; ?>/thumb1.1.jpg" alt="thumb" />
                                                                                                                                                                                                                                <div class="content">
                                                                                                                                                                                                                                    <p><a href="<?php echo $cy_value["country_id"]; ?>" rel="<?php echo $cy_value["country_id"]; ?>" class="name"><?php echo $cy_value["country_name"]; ?></a></p>
                                                                                                                                                                                                                                    <p class="desc"><?php echo $cy_value["category_name"]; ?></p>
                                                                                                                                                                                                                                </div>
                                                                                                                                                                                                                                <div class="cf"></div>
                                                                                                                                                                                                                            </div>
                        <?php
                    }
                ?>
                                    <input type="hidden" name="country_id" value=""/>
                                </div>
                            </li>-->
                <li class="zip">
                    <input class="title input-placeholder" type="text" id="address" name="location"  placeholder="City/State/Country" value="<?php echo $location; ?>"/>
                </li>
                <!--                <li class="zip">
                                    <input class="title" type="text" id="country_id" name="country_id"  placeholder="Country" value="<?php echo $country_name; ?>"/>
                                </li>
                                <li class="zip">
                                    <input class="title" type="text" id="city_name" name="city_name"  placeholder="City" value="<?php echo $city_name; ?>"/>
                                </li>-->
            </ul>
            <div class="cf"></div>
            <div class="search-box">
                <input type="text" name="query" class="txt-search" onfocus="if (this.value == this.title)
                            this.value = '';
                        this.style.fontStyle = 'normal';
                        this.style.color = '#000'" onblur="if (this.value == '') {
                            this.value = this.title;
                            this.style.fontStyle = 'italic';
                            this.style.color = '#E8962E'
                        }" value="<?php echo $query_value; ?>" title="Search" />
                <input type="submit" class="go" value="" />
            </div>
        </div>
    </form>
</div>

<script>
                    $(document).ready(function() {
                        $('input#country_id').typeahead({
                            name: 'country_id',
                            prefetch: '<?php echo $this->baseUrl("ajax/getallcountriescsv"); ?>',
                        });

                        $('input#city_name').typeahead({
                            name: 'city_name',
                            prefetch: '<?php echo $this->baseUrl("ajax/getallcitiescsv"); ?>',
                        });

                        $(".input-placeholder").keyup(function() {
                            var test_val = $(this).val();
                            if (test_val != "")
                            {
                                $(this).css("color", "#000000");
                            }
                            else
                            {
                                $(this).css("color", "#B5B5B6");
                            }
                        });

                        function reloadMe()
                        {
                            $('.dropdown .item a').live("click", function(e) {

                                e.preventDefault();
                                var li = $(this).closest("li");
                                var str = $(this).find(".name").html();
                                var value = $(this).find(".name").attr("rel");

                                $(this).parent().find("input").val(value);
                                $(this).parent().parent().find("a.title").html(str);
                                $(this).find(".title").html(str);

                                li.removeClass('opened');
                                li.find('.dropdown').slideUp();

                            });
                        }

                        $('.dropdown .item a').live("click", function(e) {

                            e.preventDefault();
                            var li = $(this).closest("li");
                            var str = $(this).find(".name").html();
                            var value = $(this).find(".name").attr("rel");

                            $(this).parent().find("input").val(value);
                            $(this).parent().parent().find("a.title").html(str);
                            $(this).find(".title").html(str);

                            li.removeClass('opened');
                            li.find('.dropdown').slideUp();

                        });

                        $(".search-box-categories").click(function() {
                            var cat_id = $(this).attr("rel");
                            var subcat_title = $(".subcat-title").html();
                            $(".subcat-title").html("Loading...");
                            $.ajax({
                                type: "POST",
                                url: "<?php echo $this->baseUrl("ajax/getsubcategoriesforsearch"); ?>",
                                data: {"cat_id": cat_id},
                                success: function(response) {
                                    $(".search-subcategories-here").html(response);
                                    reloadMe();
//                                    $(".subcat-title").html(subcat_title);
                                    $(".subcat-title").html("Subcategory");
                                }
                            });
                        });

//                        $(".subcats").live("click", function() {
//                            event.preventDefault();
//                            var subcat_id = $(this).attr("rel");
//                            $("#subcat_id").val(subcat_id);
//                        });
                    });

</script>